Imports Microsoft.Win32

<System.Runtime.InteropServices.ComVisible(False)> Class DOTNETDetect

   Private Declare Auto Function GetSystemMetrics Lib "user32" (ByVal value As Integer) As Integer

   Private Const SM_TABLETPC As Integer = 86
   Private Const SM_MEDIACENTER As Integer = 87

   ' Constants that represent registry key names and value names to use for detection
   Private Const NET_REG_KEY_NAME As String = "Software\Microsoft\.NETFramework\Policy"
   Private Const NET_FX_10_REG_KEY_NAME As String = "Software\Microsoft\.NETFramework\Policy\v1.0"
   Private Const NET_FX_10_REG_KEY_VALUE As String = "3705"
   Private Const NET_FX_10_SPx_MSI_REG_KEY_NAME As String = "Software\Microsoft\Active Setup\Installed Components\{78705f0d-e8db-4b2d-8193-982bdda15ecd}"
   Private Const NET_FX_10_SPx_OCM_REG_KEY_NAME As String = "Software\Microsoft\Active Setup\Installed Components\{FDC11A6F-17D1-48f9-9EA3-9051954BAA24}"
   Private Const NET_FX_10_SPx_REG_KEY_VALUE As String = "Version"
   Private Const NET_FX_11_REG_KEY_NAME As String = "Software\Microsoft\NET Framework Setup\NDP\v1.1.4322"
   Private Const NET_FX_11_SPx_REG_KEY_NAME As String = "Software\Microsoft\NET Framework Setup\NDP\v1.1.4322"
   Private Const NET_FX_11_REG_KEY_VALUE As String = "Install"
   Private Const NET_FX_11_SPx_REG_KEY_VALUE As String = "SP"

   Public Shared Function IsInstalled() As Boolean

      Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey(NET_REG_KEY_NAME, False)
      Return Not key Is Nothing

   End Function

   ''' <COMMENT>
   ''' Description: Uses the detection method recommended at
   ''' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetdep/html/dotnetfxref.asp
   ''' to determine whether the .NET Framework 1.0 is installed on the machine
   ''' Inputs: None
   ''' Results: True if the .NET Framework 1.0 is installed, False otherwise
   ''' </COMMENT>
   Public Shared Function IsNetfx10Installed() As Boolean

      Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey(NET_FX_10_REG_KEY_NAME, False)
      If Not key Is Nothing Then
         Dim value As String = CType(key.GetValue(NET_FX_10_REG_KEY_VALUE, ""), String)
         key.Close()
         Return (value.Length > 0)
      Else
         Return False
      End If

   End Function

   ''' <COMMENT>
   ''' Description: Uses the detection method recommended at
   ''' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetdep/html/redistdeploy1_1.asp
   ''' to determine whether the .NET Framework 1.1 is installed on the machine
   ''' Inputs: None 
   ''' Results: True if the .NET Framework 1.1 is installed, False otherwise
   ''' </COMMENT>
   Public Shared Function IsNetfx11Installed() As Boolean

      Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey(NET_FX_11_REG_KEY_NAME, False)
      If Not key Is Nothing Then
         Dim value As Integer = CType(key.GetValue(NET_FX_11_REG_KEY_VALUE, 0), Integer)
         key.Close()
         Return (value = 1)
      Else
         Return False
      End If

   End Function

   ''' <COMMENT>
   ''' Description: Uses the detection method recommended at
   ''' http://blogs.msdn.com/astebner/archive/2004/09/14/229802.aspx
   ''' to determine what service pack for the .NET Framework 1.0 is installed on the machine
   ''' Inputs: None
   ''' Results: Integer representing SP level for .NET Framework 1.0
   ''' </COMMENT>
   Public Shared Function GetNetfx10SPLevel() As Integer

      Dim pszSPLevel As String = Nothing
      Dim result As Integer = -1

      ' Need to detect what OS we are running on so we know what registry key 
      ' to use to look up the SP level.
      Dim key As RegistryKey
      If IsCurrentOSTabletMedCenter() Then
         key = Registry.LocalMachine.OpenSubKey(NET_FX_10_SPx_OCM_REG_KEY_NAME, False)
      Else
         key = Registry.LocalMachine.OpenSubKey(NET_FX_10_SPx_MSI_REG_KEY_NAME, False)
      End If
      If Not key Is Nothing Then
         Dim value As String = CType(key.GetValue(NET_FX_10_SPx_REG_KEY_VALUE, ""), String)
         key.Close()

         If value.Length > 0 Then
            ' This registry value should be of the format #,#,#####,# where the last # is the SP level.
            ' We are just interested in the #.
            Dim values() As String = Split(value, ",")
            If values.Length = 4 Then
               Return CInt(values(3))
            End If
         End If
      End If

      Return result

   End Function

   ''' <COMMENT>
   ''' Description: Uses the detection method recommended at
   ''' http://blogs.msdn.com/astebner/archive/2004/09/14/229574.aspx
   ''' to determine what service pack for the .NET Framework 1.1 is installed on the machine
   ''' Inputs: NONE()
   ''' Results: Integer representing SP level for .NET Framework 1.1
   ''' </COMMENT>
   Public Shared Function GetNetfx11SPLevel() As Integer

      Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey(NET_FX_11_SPx_REG_KEY_NAME, False)
      Dim value As Integer = CType(key.GetValue(NET_FX_11_SPx_REG_KEY_VALUE, -1), Integer)
      key.Close()

      Return value

   End Function

   Public Shared Function IsCurrentOSTabletMedCenter() As Boolean
      ' Use GetSystemMetrics to detect if we are on a Tablet PC or Media Center OS  
      Return (GetSystemMetrics(SM_TABLETPC) <> 0 OrElse GetSystemMetrics(SM_MEDIACENTER) <> 0)
   End Function

End Class
